import json
import random
from pathlib import Path
from typing import List


class JokeGenerator:
    def __init__(self, json_path: str = "data/jokes.json"):
        self.json_path = Path(json_path)
        self.jokes = []
        self.load_jokes()

    def load_jokes(self):
        """加载笑话数据"""
        try:
            with open(self.json_path, 'r', encoding='utf-8') as f:
                data = json.load(f)
                self.jokes = data.get('jokes', [])
        except Exception as e:
            print(f"加载笑话文件失败: {e}")
            self.jokes = []

    def get_random_joke(self) -> str:
        """获取单个随机笑话"""
        return random.choice(self.jokes) if self.jokes else "今天也要保持好心情哦~"

    def get_jokes(self, num: int = 1) -> List[str]:
        """获取指定数量的随机笑话"""
        if num <= 0:
            return []
        return random.sample(
            self.jokes, min(num, len(self.jokes))) if self.jokes else ["保持微笑，生活会更美好~"]